﻿using System;
using System.Collections.Generic;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace VivendoByteToolkit.Toast
{
    public static class ToastHelper
    {
        static ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
        static Dictionary<ToastTemplateType, XmlDocument> cache = new Dictionary<ToastTemplateType, XmlDocument>();

        public static bool ScheduleToastText01(string toastId, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(content));

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastText02(string toastId, string firstRow, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(firstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastText03(string toastId, string boldFirstRow, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText03);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(boldFirstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastText04(string toastId, string boldFirstRow, string content1, string content2, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(boldFirstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content1));
                toastTextElements[2].AppendChild(toastXml.CreateTextNode(content2));

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastImageAndText01(string toastId, string image, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(content));

                toastTextElements = toastXml.GetElementsByTagName("image");
                toastTextElements[0].Attributes.GetNamedItem("src").NodeValue = image;

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastImageAndText02(string toastId, string image, string boldFirstRow, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(boldFirstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

                toastTextElements = toastXml.GetElementsByTagName("image");
                toastTextElements[0].Attributes.GetNamedItem("src").NodeValue = image;

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastImageAndText03(string toastId, string image, string boldFirstRow, string content, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText03);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(boldFirstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

                toastTextElements = toastXml.GetElementsByTagName("image");
                toastTextElements[0].Attributes.GetNamedItem("src").NodeValue = image;

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ScheduleToastImageAndText04(string toastId, string image, string boldFirstRow, string content1, string content2, DateTime schedule)
        {
            try
            {
                var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
                XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
                toastTextElements[0].AppendChild(toastXml.CreateTextNode(boldFirstRow));
                toastTextElements[1].AppendChild(toastXml.CreateTextNode(content1));
                toastTextElements[2].AppendChild(toastXml.CreateTextNode(content2));

                toastTextElements = toastXml.GetElementsByTagName("image");
                toastTextElements[0].Attributes.GetNamedItem("src").NodeValue = image;

                Schedule(toastXml, schedule, toastId);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private static void Schedule(XmlDocument toastXml, DateTime schedule, string toastId)
        {
            ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml, schedule);
            scheduledToast.Id = toastId;
            notifier.AddToSchedule(scheduledToast);
        }
    }
}